package com.tunan.lsj.business.doorCard.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.EasyUiPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tunan.common.Result;
import com.tunan.common.resultSub.ResultFailure;
import com.tunan.common.resultSub.ResultSuccess;
import com.tunan.lsj.business.doorCard.service.MpbService;
import com.tunan.persistence.model.Mpb;
import com.tunan.system.service.IdentifierGenerator;
import com.tunan.system.web.BaseController;
@RestController
public class MpbController extends BaseController{
	private Logger log=Logger.getLogger(Mpb.class);
	
	@Autowired
	private MpbService mpbService;
	
	@Autowired
	private IdentifierGenerator identifierGenerator;

	
	/**
	 * 门牌列表
	 * @return
	 */
	@RequestMapping(value="/lsj/doorCard/mpList",method=RequestMethod.GET)
	public ModelAndView mpList(){
		return new ModelAndView("/lsj/doorCard/mp-list");
	}
	
	/**
	 * 查询
	 * @param pageParams
	 * @param wfds
	 * @return
	 */
	@RequestMapping(value = "/lsj/doorCard/search", method = RequestMethod.POST)
	public EasyUiPage search(@ModelAttribute("pageParams") Page pageParams,String wfds) {
		PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("wfds", wfds);
		Page pageresult = (Page) mpbService.selectAll(params);
		return pageresult.getEasyUiPage();
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/lsj/doorCard/showModify", method = RequestMethod.GET)
	public ModelAndView showModify(String id){
		ModelAndView model = new ModelAndView();
		try {
			if(StringUtils.isNotBlank(id)){
				Mpb form = mpbService.selectOne(id);
				model.addObject("vo",form);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		model.setViewName("/lsj/doorCard/mp-edit");
		return model;
	}
	
	
	@RequestMapping(value = "/lsj/doorCard/save", method = RequestMethod.POST)
	@ResponseBody
	public Result save(@ModelAttribute("mpb") Mpb mpb) {
		ResultSuccess rs = new ResultSuccess();
		ResultFailure rf = new ResultFailure();
		try {
			if (mpb != null) {
				if (StringUtils.isNotBlank(mpb.getMpid())) {
					mpbService.update(mpb);
					rs.setMessage("修改成功");
				} else {
					mpb.setMpid(identifierGenerator.generate());
					mpbService.insert(mpb);
					rs.setMessage("新增成功");
				}
			}
			return rs;

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		rf.setMessage("操作失败");
		return rf;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/lsj/doorCard/delete", method = RequestMethod.POST)
	@ResponseBody
	public Result delete(String id) {
		try {
			if (StringUtils.isNotBlank(id)) {
				mpbService.delete(id);
			}
			return new ResultSuccess("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return new ResultFailure("删除失败");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/lsj/doorCard/batchDel", method = RequestMethod.POST)
	public Result batchDel(String ids) {
		try {
			if (StringUtils.isNotBlank(ids)) {
				List<String> idList = new ArrayList<String>();
				if (ids.contains(",")) {
					idList = Arrays.asList(ids.split(","));
				} else {
					idList.add(ids);
				}
				mpbService.batchDel(idList);
			}
			return new ResultSuccess("批量删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return new ResultFailure("批量删除失败");
	}
	
}
